package User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Userlogin extends JFrame {
    JPanel root;
    JLabel userNameLabel, passWordLabel;
    JTextField userTextField;
    JButton enterButton, registerButton;
    JPasswordField passWordTextField;

    private static final String URL = "jdbc:mysql://localhost:3306/bookmanagement";
    private static final String USER = "root";
    private static final String PASSWORD = "root";

    public Userlogin() {
        root = new JPanel();
        setContentPane(root);
        setLayout(null);         //设置面板为绝对布局

        //用户名标签
        userNameLabel = new JLabel("用户名：");
        userNameLabel.setBounds(52, 33, 54, 15);
        root.add(userNameLabel);

        //用户名文本框
        userTextField = new JTextField(12);
        userTextField.setBounds(116, 30, 139, 21);
        root.add(userTextField);

        //密码标签
        passWordLabel = new JLabel("密 码：");       //定义标签对象
        passWordLabel.setBounds(52, 74, 54, 15);
        root.add(passWordLabel);

        //密码文本框
        passWordTextField = new JPasswordField(12);
        passWordTextField.setBounds(116, 71, 139, 21);
        passWordTextField.setEchoChar('●');
        root.add(passWordTextField);

        //登录按钮
        enterButton = new JButton("登录");          //定义按钮对象
        enterButton.setBounds(64, 116, 69, 23);
        root.add(enterButton);

        //退出按钮
        registerButton = new JButton("注册");
        registerButton.setBounds(174, 116, 69, 23);
        root.add(registerButton);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(400, 300, 340, 256);
        setVisible(true);

        enterButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                String username = userTextField.getText();
                char[] password = passWordTextField.getText().toCharArray();

                if (authenticate(username, new String(password))) {
                    LibraryManagementSystemHomePage libHome = new LibraryManagementSystemHomePage(username);
                    libHome.setVisible(true);
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误");
                }
            }
        });
        registerButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
              Register register = new Register();
              register.setVisible(true);
              dispose();
            }
        });
    }
    private boolean authenticate(String username, String password) {
        try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
            String sql = "SELECT * FROM usercreate WHERE username = ? AND password = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                pstmt.setString(1, username);
                pstmt.setString(2, password);
                try (ResultSet rs = pstmt.executeQuery()) {
                    return rs.next();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


}

